﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Husb.Security;
using Husb.DataUtil;
using System.Web.Security;

namespace CentralOffice.WebClientTest
{
    public partial class LoginOn : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
        {

            string userName = Login1.UserName;
            string password = Login1.Password;

            //CheckBox chkRememberMeSet = Login1.FindControl("RememberMe") as CheckBox;
            //if (chkRememberMeSet != null)
            //{
            //    Login1.RememberMeSet = chkRememberMeSet.Checked;
            //}

            GenericMembershipProvider membershipProvider = Membership.Provider as GenericMembershipProvider;
            if (membershipProvider == null)
            {
                throw new Exception("MembershipProvider没有配置！");
            }

            if (membershipProvider.ValidateUser(userName, password))
            {
                e.Authenticated = true;

                MembershipUserEx user = membershipProvider.GetUser(userName, false) as MembershipUserEx;
                SetUserContextToCookie(user);

                LogWriter.WriteLogEntry(Husb.Util.LogType.SignOn, user.Name + "于" + DateTime.Now.ToString() + " 通过测试站点登录系统。");

            }


        }

        protected void Login1_LoginError(object sender, EventArgs e)
        {

            Login l = (Login)sender;
            MembershipUser u = Membership.GetUser(l.UserName);

            if (u != null)
            {
                if (!u.IsApproved)
                {
                    l.FailureText = "帐户证在审核中";

                    //是否需要记录日志
                    //if (SwanContext.LoggingSettings.EnableLogging)
                    //{
                    //Utils.WriteLogEntry(LogType.LogOnLog, u.UserName + "帐户证在审核中");
                    //}

                    return;
                }

                if (u.IsLockedOut)
                {
                    l.FailureText = "您的帐户已经被锁定";

                    //是否需要记录日志
                    //if (SwanContext.LoggingSettings.EnableLogging)
                    //{
                    //Utils.WriteLogEntry(LogType.LogOnLog, u.UserName + "帐户已经被锁定");
                    //}

                    return;
                }
            }
            // 默认错误消息提示
            l.FailureText = "登录失败，请重试。";

            //记录日志（怎样判断登录或登出日志）
            //if (SwanContext.LoggingSettings.EnableLogging)
            //{
            //Utils.WriteLogEntry(LogType.LogOnLog, u.UserName + "登录失败");
            //}
            //WriteLogEntry(userName, false);
        }



        #region Helper
        private void SetUserContextToCookie(MembershipUserEx user)
        {
            SiteUtil.SetCookie(SiteConstants.UserId, user.UserId.ToString());
            SiteUtil.SetCookie(SiteConstants.UserName, user.UserName.ToString());

            if (user.IsEmployee)
            {
                if (user.EnterpriseId != null && user.EnterpriseId != Guid.Empty)
                {
                    SiteUtil.SetCookie(SiteConstants.ContactId, user.EmployeeId.ToString(), false);
                    SiteUtil.SetCookie(SiteConstants.Contact, user.Name, true);
                    SiteUtil.SetCookie(SiteConstants.EnterpriseId, user.EnterpriseId.ToString(), false);
                    SiteUtil.SetCookie(SiteConstants.Enterprise, user.Enterprise, true);
                    SiteUtil.SetCookie(SiteConstants.UserType, "2", false);

                    SiteUtil.DeleteCookie(SiteConstants.EmployeeId);
                    SiteUtil.DeleteCookie(SiteConstants.Employee);
                    SiteUtil.DeleteCookie(SiteConstants.DepartmentId);
                    SiteUtil.DeleteCookie(SiteConstants.Department);
                }
                else
                {
                    SiteUtil.SetCookie(SiteConstants.EmployeeId, user.EmployeeId.ToString());
                    SiteUtil.SetCookie(SiteConstants.Employee, user.Name);
                    SiteUtil.SetCookie(SiteConstants.DepartmentId, user.DepartmentId.ToString(), false);
                    SiteUtil.SetCookie(SiteConstants.Department, user.Department, true);
                    SiteUtil.SetCookie(SiteConstants.UserType, "1", false);

                    SiteUtil.DeleteCookie(SiteConstants.ContactId);
                    SiteUtil.DeleteCookie(SiteConstants.Contact);
                    SiteUtil.DeleteCookie(SiteConstants.EnterpriseId);
                    SiteUtil.DeleteCookie(SiteConstants.Enterprise);
                }
            }
            else
            {

                SiteUtil.SetCookie(SiteConstants.UserType, "0", false);

                SiteUtil.DeleteCookie(SiteConstants.EmployeeId);
                SiteUtil.DeleteCookie(SiteConstants.Employee);
                SiteUtil.DeleteCookie(SiteConstants.DepartmentId);
                SiteUtil.DeleteCookie(SiteConstants.Department);

                SiteUtil.DeleteCookie(SiteConstants.ContactId);
                SiteUtil.DeleteCookie(SiteConstants.Contact);
                SiteUtil.DeleteCookie(SiteConstants.EnterpriseId);
                SiteUtil.DeleteCookie(SiteConstants.Enterprise);
            }
        }
        /// <summary>
        /// 记录登录日志
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="signResult">登录是否成功</param>
        private void WriteLogEntry(string username, bool signResult)
        {

            //if (ApplicationContext.LoggingSettings.EnableLogging)
            //{
            //    Utils.WriteLogEntry(LogType.SignOnLog, username + "登录成功");
            //}
        }
        #endregion
    }
}
